#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setCursor(Qt::BlankCursor);//隐藏鼠标指针

    /******init config********/
    init_config();

    /*face.c*/
    face_threshold = settings->value("GUARD/face_threshold").toDouble();

    /*video.c*/
    video_refresh_rate = settings->value("GUARD/video_refresh_rate").toInt();
    video_init();

    /******init rfid**********/
    rfid = new Rfid();
    connect(rfid, &Rfid::readID, this, &MainWindow::readID);

    qDebug() << "new Rfid class done.";
}

MainWindow::~MainWindow()
{
    delete ui;
    delete settings;
    delete rfid;
}

void MainWindow::on_pushButton_close_clicked()
{
    video_stop();
    this->close();
}

void MainWindow::on_pushButton_affirm_clicked()
{
    face_recognition();
}

void MainWindow::init_config()
{
    /*init config file*/
    QString iniFilePath = QCoreApplication::applicationDirPath() + "/config.ini";
    if(!QFile(iniFilePath).exists())
    {
        QMessageBox::critical(nullptr, "ERROR",
                              iniFilePath + " is not exist.");
        qCritical() << iniFilePath + " is not exist.";
    }
    settings = new QSettings(iniFilePath, QSettings::IniFormat);
    settings->setIniCodec("UTF-8");
}

void MainWindow::readID(QString id)
{
    // TODO 进入数据库检索id
    if(id == "1f6f2051")
    {
        QMessageBox::information(this, "SUCCEED", "你好，请通过");
        qDebug() << "pass: " << id;
        // TODO　开闸
    }
}

void MainWindow::rfid_startReadID()
{
    rfid->startListen();
}
